﻿<UserControl x:Class="ScrumSprintMonitor.UI.Wpf.Views.MonitorWindow.ChartLegend"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ScrumSprintMonitor.UI.Wpf"
    xmlns:converters="clr-namespace:ScrumSprintMonitor.UI.Wpf.Converters"
    xmlns:MonitorWindow="clr-namespace:ScrumSprintMonitor.UI.Wpf.Views.MonitorWindow">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\..\Styles\ChartResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <local:XamlServiceLocator x:Key="serviceLocator" />
            <converters:LegendBrushValueConverter x:Key="LegendBrushValueConverter"/>

            <Style x:Key="InfoLabel" TargetType="{x:Type TextBlock}">
                <Setter Property="FontWeight" Value="Light" />
                <Setter Property="Opacity" Value="0.7" />
            </Style>

            <Style x:Key="NumericValueLabel" TargetType="{x:Type TextBlock}">
                <Setter Property="FontStyle" Value="Italic" />
            </Style>

            <Style x:Key="UncertainValueStyle" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource NumericValueLabel}">
                <Setter Property="Visibility" Value="Visible"/>

                <Style.Triggers>
                    <DataTrigger Binding="{Binding ActualValueCanBeHigher}" Value="False">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            
            <DataTemplate x:Key="HoursTemplate">
                <StackPanel Orientation="Horizontal">
                    <Rectangle Width="8" Height="8" Stroke="Black" StrokeThickness="0.5" Margin="0,0,3,0">
                        <Rectangle.Fill>
                            <MultiBinding Converter="{StaticResource LegendBrushValueConverter}">
                                <Binding Path="." />
                                <Binding Path="ItemsSource" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}" />
                                <Binding Path="." RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type MonitorWindow:ChartLegend}}" />
                            </MultiBinding>
                        </Rectangle.Fill>
                    </Rectangle>
                    <TextBlock Text="{Binding StateName, StringFormat=\{0\}\:}" Style="{StaticResource InfoLabel}" />
                    <TextBlock Text="{Binding Value}" Margin="6,0,0,0" Style="{StaticResource NumericValueLabel}" />
                    <TextBlock Text="h" Style="{StaticResource NumericValueLabel}" />
                    <TextBlock Text="+" Style="{StaticResource UncertainValueStyle}" />
                </StackPanel>
            </DataTemplate>

            <DataTemplate x:Key="ItemCountTemplate">
                <StackPanel Orientation="Horizontal">
                    <Rectangle Width="8" Height="8" Stroke="Black" StrokeThickness="0.5" Margin="0,0,3,0">
                        <Rectangle.Fill>
                            <MultiBinding Converter="{StaticResource LegendBrushValueConverter}">
                                <Binding Path="." />
                                <Binding Path="ItemsSource" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}" />
                                <Binding Path="." RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type MonitorWindow:ChartLegend}}" />
                            </MultiBinding>
                        </Rectangle.Fill>
                    </Rectangle>
                    <TextBlock Style="{StaticResource InfoLabel}" Text="{Binding StateName, StringFormat='{}{0}:'}" />
                    <TextBlock Style="{StaticResource NumericValueLabel}" Margin="3,0,0,0" Text="{Binding Value}" />
                    <TextBlock Text="+" Style="{StaticResource UncertainValueStyle}" />
                    <TextBlock Opacity="0.6" Margin="3,0,0,0">
                        <TextBlock.Style>
                            <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource NumericValueLabel}">
                                <Setter Property="Visibility" Value="Visible" />

                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Value}" Value="0">
                                        <Setter Property="Visibility" Value="Collapsed" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                        
                        <TextBlock.Text>
                            <Binding Path="AverageTimePerItem" StringFormat="({0:N1}h/item)" />
                        </TextBlock.Text>
                    </TextBlock>
                </StackPanel>
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>

    <UserControl.DataContext>
        <Binding Path="BacklogItemCountChartViewModel.PieChartData" Source="{StaticResource serviceLocator}"/>
    </UserControl.DataContext>

    <ItemsControl x:Name="Legend" ItemsSource="{Binding}" Focusable="False">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding}">
                    <ContentPresenter.Style>
                        <Style TargetType="{x:Type ContentPresenter}">
                            <Setter Property="ContentTemplate" Value="{StaticResource HoursTemplate}" />

                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ValueIsHours}" Value="False">
                                    <Setter Property="ContentTemplate" Value="{StaticResource ItemCountTemplate}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding StateName}" Value="">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ContentPresenter.Style>
                </ContentPresenter>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</UserControl>
